<template>
    <div class="subProject">
        <template v-if="active">
            <component
                ref="component"
                v-for="(item,index) in componentsList"
                :key="index"
                :is="item.cKey"
                :disabled.sync="item.disabled"
                :info.sync="info"
                :approvalInfo="getCurrentApproval(info.flows,item.key)"
                :rejectFlows="getCurrentReject(info.rejectFlows,item.key)"
                :expand="item.expand"
                :id="item.cKey"
                :class="{isOperation: (active===item.key && active !=='Post Calculation') || (!active && item.key==='Spec. In')}"
            >
            </component>
        </template>
    </div>
</template>

<script>

import bidding from "@/views/devBasic/capex/components/bidding";
// import biddingApproval from "@/views/devBasic/capex/components/biddingApproval";
import confirmVendor from "@/views/devBasic/capex/components/confirmVendor";
import confirmVendorApproval from "@/views/devBasic/capex/components/confirmVendorApproval";
import specOut from "@/views/devBasic/capex/components/specOut";
import specOutApproval from "@/views/devBasic/capex/components/specOutApproval";
import contractSignOff from "@/views/devBasic/capex/components/contractSignOff";
import contractSignOffApproval from "@/views/devBasic/capex/components/contractSignOffApproval";
import pr from "@/views/devBasic/capex/components/pr";
// import prApproval from "@/views/devBasic/capex/components/prApproval";
import downPayment from "@/views/devBasic/capex/components/downPayment";
import downPaymentApproval from "@/views/devBasic/capex/components/downPaymentApproval";
import sat from "@/views/devBasic/capex/components/sat";
import satApproval from "@/views/devBasic/capex/components/satApproval";
import cat from "@/views/devBasic/capex/components/cat";
import catApproval from "@/views/devBasic/capex/components/catApproval";
import fat from "@/views/devBasic/capex/components/fat";
import fatApproval from "@/views/devBasic/capex/components/fatApproval";
import capitalization from "@/views/devBasic/capex/components/capitalization";
import downPayment2 from "@/views/devBasic/capex/components/downPayment2";
import downPaymentApproval2 from "@/views/devBasic/capex/components/downPaymentApproval2";
import downPayment3 from "@/views/devBasic/capex/components/downPayment3";
import downPaymentApproval3 from "@/views/devBasic/capex/components/downPaymentApproval3";
// import capitalizationApproval from "@/views/devBasic/capex/components/capitalizationApproval";

import capitalizationApproval from "@/views/devBasic/capex/components/capitalizationApproval";
import mixins from "@/views/devBasic/capex/components/mixins";

function getComponents() {
    return [
        // {cKey: "bidding", key: "Bidding", approval: 0},
        // {cKey: "confirmVendor", key: "Confirm Vendor", approval: 0, processCode: "confirmVendor"},
        // {cKey: "confirmVendorApproval", key: "Confirm Vendor Approval", approval: 1, processCode: "confirmVendor"},
        {cKey: "specOut", key: "Spec. Out", approval: 0},
        // {cKey: "specOutApproval", key: "O", approval: 1},
        {cKey: "contractSignOff", key: "Contract Sign-off", approval: 0},
        // {cKey: "contractSignOffApproval", key: "S", approval: 1},
        {cKey: "pr", key: "PR", approval: 0},
        // {cKey: "prApproval", key: "R", approval: 1},
        {cKey: "downPayment", key: "Down Payment", approval: 0},
        {cKey: "downPaymentApproval", key: "Down Payment Approval", approval: 1},
        {cKey: "sat", key: "SAT", approval: 0, processCode: "SAT"},
        {cKey: "satApproval", key: "SAT Approval", approval: 1, processCode: "SAT"},
        {cKey: "cat", key: "CAT", approval: 0, processCode: "CAT"},
        {cKey: "catApproval", key: "CAT Approval", approval: 1, processCode: "CAT"},
        {cKey: "downPayment2", key: "2nd Payment", approval: 0, processCode: "downPayment2"},
        {cKey: "downPaymentApproval2", key: "2nd Payment Approval", approval: 1, processCode: "downPayment2"},
        {cKey: "fat", key: "FAT", approval: 0, processCode: "FAT"},
        {cKey: "fatApproval", key: "FAT Approval", approval: 1, processCode: "FAT"},
        {cKey: "downPayment3", key: "3nd Payment", approval: 0, processCode: "downPayment3"},
        {cKey: "downPaymentApproval3", key: "3nd Payment Approval", approval: 1, processCode: "downPayment3"},
        {cKey: "capitalization", key: "Capitalization", approval: 0, processCode: "Capitalization"},
        {cKey: "capitalizationApproval", key: "Capitalization Approval", approval: 1, processCode: "Capitalization"},
    ]
}

export default {
    name: "operation",
    props: ['components', 'row', 'isActive', 'mainInfo'],
    mixins: [mixins],
    components: {
        bidding,
        // biddingApproval,
        // confirmVendor,
        // confirmVendorApproval,
        specOut,
        specOutApproval,
        contractSignOff,
        contractSignOffApproval,
        pr,
        // prApproval,
        downPayment,
        downPaymentApproval,
        sat,
        satApproval,
        cat,
        catApproval,
        fat,
        fatApproval,
        capitalization,
        capitalizationApproval,
        downPayment2,
        downPaymentApproval2,
        downPayment3,
        downPaymentApproval3,
        confirmVendor,
        confirmVendorApproval
    },
    data() {
        return {
            active: "",
            approval: '',
            componentsList: [],
            refIndex: "",
            info: {
                orderProcessStatus: 0,
                flows: [],
                displayButton: 0
            },
            isEnd: false
        }
    },
    watch: {
        isActive: function (val) {
            if (val) {
                // debugger
                this.getInfo()
            }
        }
    },
    mounted() {
        if (this.isActive) {
            this.getInfo()
        }
        // this.getCurrentComponent('O',1)
        // this.active = 'O'
    },
    methods: {
        getCurrentComponent(code) {
            let cvRepeatTaggingStatus = false

            const list = this.getProcessLayoutList(getComponents(), this.info)


            // if (this.info.bdgApprovalFlag===1){
            //   list.splice(list.length-1)
            //   list[list.length - 1]['expand'] = true
            //  }

            this.refIndex = (list.length - 1) / 1
            // const cvStatus = this.info.progressCode === 'Confirm Vendor' && (this.info.cvRepeatTagging == 2 || this.info.cvRepeatTagging == 1)
            // if () {
            // list.splice(list.length - 1, 1)
            // list[list.length - 1]['disabled'] = false

            // this.refIndex--
            // }

            this.componentsList = list;
            this.approval = list[list.length - 1]['approval']
            this.active = list[list.length - 1]['key']


            // if (cvStatus) {
            //     cvRepeatTaggingStatus = true
            //     list[list.length - 1]['disabled'] = true
            //     list[list.length - 1]['expand'] = true
            //     list[list.length - 2]['disabled'] = false
            //     this.canRollback = false
            //     this.refIndex--
            //     this.approval = list[list.length - 2]['approval']
            //     this.active = list[list.length - 2]['key']
            //
            // }



            if (this.approval == 1 && list[list.length - 1]['processCode'] === list[list.length - 2]['processCode'] && this.info.progressCode !== "Capitalization Approval") {
                // if (this.info.bdgApprovalFlag!=1){
                list[list.length - 2]['expand'] = true
                // }
            }

            // if (this.info.clockSubProject){
            //   list.splice(list.length-1)
            //   list[list.length - 1]['expand'] = false
            // }

            if (this.info.progressCode === "Post Calculation") {
                this.isEnd = true
            }

            this.componentsList = list

            this.$emit('onMessage', {...this.info, active: this.active, id: this.row.id, list, cvRepeatTaggingStatus})

            this.$forceUpdate()
        },
        getInfo() {
            this.axios({
                method: 'get',
                url: '/dms/capex/findById',
                params: {id: this.row.id}
            }).then(res => {
                if (res.code === 200) {
                    let info = res.data;
                    const cacheInfo = this.getCacheData(info)
                    info = {...info,...cacheInfo}
                    // if (info.progressCode === 'Spec. Out') info.progressCode = ''
                    if (!info.progressCode) {
                        this.componentsList = [getComponents()[0]]
                        this.active = this.componentsList[0]['key']
                        this.approval = 0
                        this.refIndex = 0
                        this.info = {
                            biddings: info.biddings,
                            orderProcessStatus: 0,
                            flows: [],
                            biddingIsRequired: 0,
                            satIsRequired: 0,
                            twoPaymentBeforeCat: 0,
                            ...info,
                            ...cacheInfo
                        }


                        this.$emit('onMessage', {
                            ...this.info,
                            active: this.active,
                            id: this.row.id,
                            list: this.componentsList
                        })

                    } else {
                        this.info = info;
                        /**
                         * 主流程数据 部分同步到子项目
                         */


                        this.info.orgName = this.mainInfo.orgName
                        // this.info.bdgApprovalFlag = this.mainInfo.bdgApprovalFlag
                        this.info.exceedsBudget = this.mainInfo.exceedsBudget
                        this.info.mainProjectId = this.mainInfo.mainProjectId
                        this.info.mainCompanyName = this.mainInfo.companyName
                        this.info.mainOrgId = this.mainInfo.orgId
                        this.info.cvInfo = this.mainInfo.cvInfo
                        // this.info.assetNo = this.mainInfo.assetNo
                        this.info.cvDetails = this.mainInfo.cvDetails

                        // this.info.clockSubProject = this.mainInfo.clockSubProject
                        // const CVApprovals = this.mainInfo.capexSubmitPos.find(item=> item.cvApprovalFinishFlag ==  1)
                        // const BDGApprovalStatus = this.mainInfo.bdgApprovalInfos.find(item => item.status==0)
                        // if (CVApprovals && CVApprovals.id == this.info.id && BDGApprovalStatus){
                        //   this.info.clockSubProject = true
                        // }else{
                        //   this.info.clockSubProject = false
                        // }

                        this.getCurrentComponent(this.info.progressCode)
                    }


                    this.$store.commit('capexProcessSchedule', {
                        active: this.info.progressCode === 'Capitalization Approval' ? this.$store.state.capexProcessSchedule.active : this.active,
                        flows: [...(this.$store.state.capexProcessSchedule.mainFlows || []), ...(this.info.flows || [])],
                        isEnd: this.$store.state.capexProcessSchedule.isEnd ? this.$store.state.capexProcessSchedule.isEnd : this.isEnd,
                        biddingIsRequired: this.info.biddingIsRequired,
                        satIsRequired: this.info.satIsRequired,
                        twoPaymentBeforeCat: this.info.twoPaymentBeforeCat,
                        mainFlows: this.$store.state.capexProcessSchedule.mainFlows,
                        render: true,
                        mainId: this.$store.state.capexProcessSchedule.mainId,
                        id: this.info.id,
                        mainProjectId: this.$store.state.capexProcessSchedule.projectId,
                        subProjectId:  this.info.projectId,
                    })


                } else {
                    this.$Message.warning(({content: res.msg, duration: 6, closable: true}));
                }
            })
        },
        async getParams(status) {
            const params = await this.$refs.component[this.refIndex].getParams(status)
            if (!params) return false
            return {...params, progressCode: this.active, id: this.row.id, approval: this.approval}
        }
    },

}
</script>
<style lang="scss">
.subProject {
    padding: 16px 5px 5px;

    .detail-content-layout {
        &:last-child {
            margin-bottom: 0 !important;
        }
    }

}
</style>
